package org.example.services.impl;

import ch.qos.logback.classic.Logger;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.weaver.ast.Or;
import org.example.Exception.RollBackException;
import org.example.dao.GoodsDao;
import org.example.dao.OrderDao;
import org.example.dao.UserDao;
import org.example.domain.Goods;
import org.example.domain.Order;
import org.example.domain.PageBean;
import org.example.domain.User;
import org.example.domain.response.ResponseResult;
import org.example.services.OrderService;
import org.example.utils.PageConvertPageBeanUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Transactional
@Service
public class OrderServiceImpl implements OrderService {

    @Resource
    private OrderDao orderDao;
    @Resource
    private UserDao userDao;
    @Resource
    private GoodsDao goodsDao;



    @Override
    public ResponseResult saveOrder(Order order) {
        try {
            Goods goods = goodsDao.selectById(order.getGoodsId());
            if(goods==null){
                log.info("新增商品：商品不存在");
                return ResponseResult.failed("商品不存在");
            }
            User user = userDao.selectById(order.getUserId());
            if(user==null){
                log.info("新增订单：用户ID不存在");
                return ResponseResult.failed("用户不存在");
            }
            orderDao.insert(order);
            log.info("新增订单，成功{}",order);
            return ResponseResult.success("新增订单成功",order);
        }catch (Exception e){
            e.printStackTrace();
            log.info("新增订单失败");
            throw new RollBackException("新增订单失败");
        }
    }

    @Override
    public ResponseResult updateOrder(Order order) {
        Order orderRes = orderDao.selectById(order.getOrderId());
        if(orderRes==null){
            log.warn("订单修改：订单基本信息查询失败");
            throw new RollBackException("无该订单信息，请刷新");
        }
        try {
            Goods goods = goodsDao.selectById(order.getGoodsId());
            if (goods == null) {
                log.info("更新商品：商品不存在");
                return ResponseResult.failed("商品不存在");
            }
            User user = userDao.selectById(order.getUserId());
            if (user == null) {
                log.info("更新订单：用户ID不存在");
                return ResponseResult.failed("用户不存在");
            }
            orderDao.updateById(order);
            log.info(orderRes.getOrderId()+"修改订单成功");
            return ResponseResult.success("修改订单成功");
        }catch (Exception e){
            e.printStackTrace();
            log.info("修改订单失败");
            throw new RollBackException("修改订单失败");
        }
    }

    @Override
    public ResponseResult deleteOrder(Integer orderId) {
        try {
            Order order = orderDao.selectById(orderId);
            if (order == null) {
                log.info("删除订单：订单不存在");
                return ResponseResult.failed("订单不存在");
            }
            orderDao.deleteById(orderId);
            log.info("删除订单：成功{}", order);
            return ResponseResult.success("删除订单成功", order);
             }catch (Exception e){
            e.printStackTrace();
            log.warn("删除订单：失败");
            throw new RollBackException("删除订单失败");
        }
         }
    @Override
    public ResponseResult getAllOrder() {
        List<Order> orders = orderDao.selectList(null);
        for (Order order1:orders){
            String s = orderDao.selectInfoByOrderId(order1.getOrderId());
        }
        return ResponseResult.success("查询成功",orders);
    }

    @Override
    public ResponseResult getOrderByOrderId(Integer orderId) {
        try {
            Order order = orderDao.selectById(orderId);
            String s = orderDao.selectInfoByOrderId(order.getOrderId());
            return ResponseResult.success("查询成功",order);
        }catch (NullPointerException e){
            log.warn(orderId+"，通过id查询失败，case：无该订单信息");
            throw new RollBackException("无该订单数据");
        }catch (Exception e){
            log.warn(orderId+":通过id查询失败,case:"+e.getCause().getMessage());
            throw new RollBackException("查询订单失败");
        }
    }

    @Override
    public ResponseResult getPage(int currentPage, int pageSize) {
        Page<Order> page = new Page(currentPage, pageSize);
//        封装
        Page<Order> orderPage = orderDao.selectPage(page, null);
        PageBean<Order> pageBean = PageConvertPageBeanUtil.convert(orderPage);
        List<Order> rows = pageBean.getRows();
        for (Order row : rows) {
            row.setUserName(userDao.getUserNameById(row.getUserId()));
            row.setGoodsName(goodsDao.getGoodsNameById(row.getGoodsId()));
        }
        System.out.println(rows);
        log.info("分页查询讨论：成功{}",pageBean);
        return ResponseResult.success("分页查询订单成功",pageBean);
    }

    public ResponseResult getOrderByUserId(int userId,int pageSize,int currentPage){
        try {
            Page<Order> page = new Page<>(currentPage,pageSize);
            QueryWrapper<Order> wrapper = new QueryWrapper<>();
            wrapper.like("user_id",userId);
            Page<Order> orderPage = orderDao.selectPage(page, wrapper);
            PageBean<Order> pageBean=PageConvertPageBeanUtil.convert(orderPage);
            List<Order> rows = pageBean.getRows();
            for (Order row : rows) {
                row.setUserName(userDao.getUserNameById(row.getUserId()));
                row.setGoodsName(goodsDao.getGoodsNameById(row.getGoodsId()));
            }
            log.info("按商品名称分页查询成功");
            return ResponseResult.success("分页查询名字成功",pageBean);
//            return getGoodsByPage(page,wrapper);
        } catch (Exception e) {
            e.printStackTrace();
            log.warn("按商品名称分页查询失败");
            return null;
        }
    }
}
